/*

Interval Trainer , a tool for your android phone helping with your excercise  
Copyright (C) 2009  Henrik Andersson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

package se.dinamic.android.intervaltrainer;

import android.app.Activity;
import android.util.Log;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Menu;
import android.view.MenuItem;

import android.widget.ScrollView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.ArrayList;

class WizardPage {
    private String 	_title;
    private String 	_description;
    private View         _view;
    private int          _icon;
    
    public WizardPage( String title, String description , int icon) {
        _title = title;
        _description = description;
        _icon = icon;
    }
    
    public void hide() {
        _view.setVisibility( View.INVISIBLE );
    }
    
    public void show() {
         _view.setVisibility( View.VISIBLE );
        //_view.requestLayout();
        
    }
    
    public void setView( View view ) {
        _view = view;
        Log.v("WizardPage","setView(): "+view.toString() );
        hide();
    }
    
    public String title() {
        return _title;
    }
    
    public int icon() { 
        return _icon; 
    }
    
    public String description() {
        return _title;
    }
}

public class WizardActivity extends Activity implements View.OnClickListener
{
    private int _currentPageIndex = 0;
    private final ArrayList<WizardPage> _pages = new ArrayList();
    
    private TextView _title;
    private TextView _description;
    private ImageView _icon;
    private LinearLayout _content;
    private ScrollView _scrollview;
    private LinearLayout _header;
    private LinearLayout _buttonbar;
    private LinearLayout _wizard;
    
    private Button _buttonNext;
    private Button _buttonPrev;
    private Button _buttonCancel;
    private Button _buttonOk;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wizard);
        
        // Fetch the wizard objects
        _wizard = (LinearLayout) findViewById( R.id.wizardResourceID );
        _title = (TextView) findViewById(R.id.title);
        _description = (TextView) findViewById(R.id.description);
        _icon = (ImageView) findViewById(R.id.icon);
        _content = (LinearLayout) findViewById(R.id.content);
       
        // Setup buttons
        _buttonNext = ( Button ) findViewById( R.id.button_next );
        _buttonPrev = ( Button ) findViewById( R.id.button_prev );
        _buttonOk = ( Button ) findViewById( R.id.button_ok );
        _buttonCancel = ( Button ) findViewById( R.id.button_cancel );
        
        _buttonNext.setOnClickListener( this );
        _buttonPrev.setOnClickListener( this );
        _buttonOk.setOnClickListener( this );
        _buttonCancel.setOnClickListener( this );

        
        Log.v("WizardActivity","_content = "+_content.toString() );
        
    }
    
    @Override
    public void onClick(View v) {
        if( v.equals( _buttonNext ) )
            nextPage(); 
        else if( v.equals( _buttonPrev ) )
            previousPage();
        else if( v.equals( _buttonOk ) )
            exitWizard( false );
        else if( v.equals( _buttonCancel ) )
            exitWizard( true );
        
    }
    
    public void exitWizard(boolean abort) {
        if( abort ) {
            //Toast.makeText( this , R.string.wizard_aborted, 1000).show();
            setResult( Activity.RESULT_CANCELED );
        } else {
            setResult( Activity.RESULT_OK );
        }
        
        finish();
    }
    
    public void addPage( int icon, String title, String description, int layout) {
        Log.v("WizardActivity","Adding new page to wizard named " + title );
        
        WizardPage page =  new WizardPage(  title, description, icon );
        View.inflate( this, layout , _content ) ;
        // Log.v("WizardActivity","_content has " + _content.getChildCount( ) +" childs");
        page.setView( _content.getChildAt( _content.getChildCount( )-1 ) );         // attach view of inflated layout
        _pages.add( page ); 
    }

    protected void arrange() {
        // Set first page
        setPage( ( WizardPage ) _pages.get( 0 ) );
        
        // Run thru pages and set buttons
        for( WizardPage page: _pages ) {
            // If this is first page, dont add previous button
        
        }
    }
    
    private void nextPage(  ) {
        if( _currentPageIndex <  _pages.size()  ) {
            _currentPageIndex++;
             _pages.get( _currentPageIndex-1 ).hide();
            setPage( _pages.get( _currentPageIndex ) );
        }
    }
    
    private void previousPage(  ) {
        if( _currentPageIndex > 0 ) {
            _currentPageIndex--;
            _pages.get( _currentPageIndex+1 ).hide();
            setPage( _pages.get( _currentPageIndex ) );
        }
    }
    
    private void setPage( WizardPage page ) {
        // Update header
        _title.setText( page.title() );
        _description.setText( page.description() );
        _icon.setImageResource( page.icon() );
        
        // Update visibility of buttons depending on page index
        int index = _pages.indexOf( page );
         _buttonOk.setVisibility( View.INVISIBLE );
        _buttonCancel.setVisibility( View.INVISIBLE );
        _buttonNext.setVisibility( View.INVISIBLE );
        _buttonPrev.setVisibility( View.INVISIBLE );
           
        if( index != _pages.size()-1 ) {                           // Is not the last page
           _buttonNext.setVisibility( View.VISIBLE );
           if( index > 0 ) 
               _buttonPrev.setVisibility( View.VISIBLE );
        } else {
            _buttonOk.setVisibility( View.VISIBLE );
           _buttonCancel.setVisibility( View.VISIBLE );
           _buttonPrev.setVisibility( View.VISIBLE );
        }
        
        // Show page
        page.show();
       _content.forceLayout();
    }
    
}
